package common;

public class ListNodeUtils {
    /**
     * 创建单链表
     *
     * @param arr
     * @return
     */
    public static ListNode getLinked(int... arr) {
        ListNode ans = null;
        int i = 0;
        ListNode tail = null;
        while (i < arr.length) {
            if (ans == null) {
                ans = tail = new ListNode(arr[i]);
            } else {
                tail = tail.next = new ListNode(arr[i]);
            }
            i++;
        }
        return ans;
    }


    public static DoubleListNode getDoubleLinked(int... arr) {
        int i = 0;
        DoubleListNode head = null, tail = null;
        while (i < arr.length) {
            if (head == null) {
                head = tail = new DoubleListNode(arr[i]);
            } else {
                DoubleListNode pre = tail;
                tail = tail.next = new DoubleListNode(arr[i]);
                tail.last = pre;
            }
            i++;
        }
        return head;
    }

    /**
     * 打印节点
     */
    public static void print(ListNode start) {
        while (start != null) {
            System.out.print(start.val + "\t");
            start = start.next;
        }
        System.out.println();
    }

    public static void print(DoubleListNode start) {
        while (start != null) {
            System.out.print(start.val);
            start = start.next;
            System.out.print(start != null ? "\t⇄\t" : "");
        }

        System.out.println();
    }


}
